package com.hsh.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hsh.bean.Comment;
import com.hsh.dao.CommentMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class CommentService {

    @Resource
    private CommentMapper commentMapper;

    public Integer insert(Comment comment) {
        comment.setCDate(new Date());
        comment.setIsFine(0);
        return commentMapper.insert(comment);
    }

    public Integer delete(Integer cid) {
        return commentMapper.deleteByPrimaryKey(cid);
    }

    public Integer update(Comment comment) {
        return commentMapper.updateByPrimaryKey(comment);
    }

    public Comment findByCid(Integer cid) {
        return commentMapper.findByCid(cid);
    }

    /**
     * 查询所有发表的话题
     * 即cTargetId = 0
     *
     * @param comment
     * @return
     */
    public PageInfo<Comment> findCommentByPage(Comment comment, Integer pageNum, Integer pageSize) {
        return PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(
                () -> {
                    commentMapper.findComments(comment);
                }
        );
    }

    /**
     * 查询指定话题的子话题
     *
     * @param cTargetId
     * @return
     */
    public PageInfo<Comment> findCommentInfo(Integer cTargetId, Integer pageNum, Integer pageSize) {
        return PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(
                () -> {
                    commentMapper.findCommentInfo(cTargetId);
                }
        );
    }
}
